import { BT_State } from "../../BTDefine";
import { Composite } from "./Composite";

export class Sequence extends Composite {

    protected _curIndex = 0;

    public onEnter(): void {
        this._curIndex = 0;
    }

    public onExit(): void {

    }

    public onProcess(dt: number): void {
        let startIndex = this._curIndex;

        for (let i = startIndex; i < this._children.length; i++) {
            const child = this._children[i];
            let ret = child.tick(dt);

            if (ret != BT_State.SUCCESS) {
                this._state = ret;
                break;
            }

            this._curIndex++;
        }

        if (this._state == BT_State.RUNNING && this._curIndex >= this._children.length) {
            this.setResult(BT_State.SUCCESS);
        }
    }
}